
package cn.com.iscs.sqlProject.config;


/**
 * Created by yejunjun on 2017/3/1.
 */


import cn.com.iscs.sqlProject.filter.CrossOriginFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.SQLException;


@Configuration
public class DataSourcesConfig {
/**
	 * druid初始化
	 * @return
	 * @throws SQLException
	 */

	@Primary //默认数据源
	@Bean(name = "dataSource",destroyMethod = "close")
	public DruidDataSource Construction() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl("jdbc:oracle:thin:@192.168.6.132:1521/BASICDATA");
		dataSource.setUsername("xdw_app");
		dataSource.setPassword("xdw_app#");
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		//配置最大连接
		dataSource.setMaxActive(20);
		//配置初始连接
		dataSource.setInitialSize(1);
		//配置最小连接
		dataSource.setMinIdle(1);
		//连接等待超时时间maxWait: 配置获取连接等待超时的时间 6000毫秒/1000等于60秒
		dataSource.setMaxWait(60000);

/*		removeAbandoned: 是否自动回收超时连接 可以检测连接泄露
		removeAbandonedTimeout: 超时时间(以秒数为单位)*/
		dataSource.setRemoveAbandoned(true);
		dataSource.setRemoveAbandonedTimeout(900);
		/*是否打印连接内存泄露日志*/
		dataSource.setLogAbandoned(true);

		dataSource.setDefaultAutoCommit(false);
		//间隔多久进行检测,关闭空闲连接,单位是毫秒
		dataSource.setTimeBetweenEvictionRunsMillis(60000);
		//一个连接最小生存时间
		dataSource.setMinEvictableIdleTimeMillis(300000);
		//用来检测是否有效的sql
		dataSource.setValidationQuery("select 1 from dual");
		dataSource.setTestWhileIdle(true);
		dataSource.setTestOnBorrow(false);
		dataSource.setTestOnReturn(false);

		/*执行查询与事务的超时设置*/
		dataSource.setQueryTimeout(100);
		dataSource.setTransactionQueryTimeout(200);
		//打开PSCache,并指定每个连接的PSCache大小
		dataSource.setPoolPreparedStatements(true);
		dataSource.setMaxOpenPreparedStatements(20);
		//配置sql监控的filter
		dataSource.setFilters("stat,wall,log4j");
		try {
			dataSource.init();
		} catch (SQLException e) {
			throw new RuntimeException("druid datasource init fail");
		}
		return dataSource;
	}

	/**
	 * druid监控  http://localhost:8080/druid/index.html
	 * @return
	 */
	@Bean
	public ServletRegistrationBean druidServlet() {
		ServletRegistrationBean reg = new ServletRegistrationBean();
		reg.setServlet(new StatViewServlet());
		reg.addUrlMappings("/druid/*");
		//reg.addInitParameter("allow", "127.0.0.1");
		//reg.addInitParameter("deny","");
		reg.addInitParameter("loginUsername", "admin");
		reg.addInitParameter("loginPassword", "admin");
		return reg;
	}

	/**
	 * druid监控过滤
	 * @return
	 */
	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		return filterRegistrationBean;
	}

	/**
	 * 跨域拦截器，用于跨域访问
	 * @return
	 */
	@Bean
	public FilterRegistrationBean  filterCrossOriginFilterBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		CrossOriginFilter httpBasicFilter = new CrossOriginFilter();
		registrationBean.setFilter(httpBasicFilter);
		registrationBean.addUrlPatterns("/*");
		return registrationBean;
	}
}
